package com.blt.govdata.controller;

import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TLand;
import com.blt.govdata.service.ITLandService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tLand")
//todo 将所有的XXX修改为真实值
public class TLandController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITLandService service;

    /**
     * 查询土地
     */
    @RequestMapping(value = "/showLand")

    public Map<String, Object> showLand(@RequestParam Map<String, Object> params) {
        logger.info("查询土地");
        return ImmutableMap.of("status", "true", "data", this.service.showLand(params));
    }

    /**
     * 土地新增
     * */
    @RequestMapping(value = "/addLand",params = {"propertyId","geometry"})
    public Map<String, Object> addLand(TLand land) {
        logger.info("新增土地");
        land.setIsDelete(Constants.UN_DELETED);
        return ImmutableMap.of("status", "true", "data", this.service.addLand(land));
    }

    /**
     * 土地编辑/拖拽
     * */
    @RequestMapping(value = "/editLand",params = {"id"})
    public Map<String, Object> editLand(TLand land) {
        logger.info("编辑土地");
        return ImmutableMap.of("status", "true", "data", this.service.modifyLand(land));
    }

    /**
     * 土地分割
     * geometrylf: geometrylf 更新原来的geometry
     * geometryrf: geometryrf 添加新的geometry
     * propertyId: layerid,
     * id: id （更新当前id,geometry为geometrylf）
     * */

    @RequestMapping(value = "/cutLand",params = {"id","geometrylf","geometryrf","propertyId"})
    public Map<String, Object> cutLand(Integer id,String geometrylf,String geometryrf,String propertyId) {
        TLand updateLand = BeanUtils.instantiate(TLand.class); //更新
        updateLand.setId(id);
        updateLand.setGeometry(geometrylf);
        boolean b = this.service.modifyLand(updateLand);
        if(!b) return ImmutableMap.of("status", "false", "data", "对应Id土地不存在");
        TLand addLand = BeanUtils.instantiate(TLand.class); //增加

        addLand.setGeometry(geometryrf);
        addLand.setPropertyId(propertyId);
        this.service.addLand(addLand);

        return ImmutableMap.of("status", "true", "data", addLand.getId());
    }


    /**
     * 土地合并
     * geometry: geometry,
     * id: id,（更新此id的geometry）
     * ids: ids（删除次id集合，字符串，以逗号分割）
     * */

    @RequestMapping(value = "/mergeLand",params = {"id","geometry","ids"})
    public Map<String, Object> mergeLand(Integer id,String geometry,String ids) {
        /**
         * 更新合并后的Id
         * */
        TLand updateLand = BeanUtils.instantiate(TLand.class); //更新
        updateLand.setId(id);
        updateLand.setGeometry(geometry);
        boolean a = this.service.modifyLand(updateLand);
        /**
         * 删除之前不需要的ids
         * */
        List<String> list = Arrays.asList(ids.split(","));

        list.stream().forEach(e->{
            TLand deleteLand = BeanUtils.instantiate(TLand.class); //更新
            deleteLand.setId(Integer.valueOf(e));
            deleteLand.setIsDelete(Constants.IS_DELETED);
            this.service.updateById(deleteLand);
        });

        return ImmutableMap.of("status", "true", "data", a);
    }

    /**
     * 删除土地
     * */
    @RequestMapping(value = "/deleteLand",params = {"id"})
    public Map<String, Object> deleteLand(TLand deleteLand) {

        deleteLand.setIsDelete(Constants.IS_DELETED);

        return ImmutableMap.of("status", "true", "data", this.service.updateById(deleteLand));
    }


}
